<?php

namespace app\admin\model;

use think\Model;


class HouseVisitModel extends Model
{

    

    

    // 表名
    protected $name = 'house_visit';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'create_time_text'
    ];



    public function house()
    {
        return $this->belongsTo('\app\admin\model\HouseModel', 'house_id', 'id');
    }

    public function getTagsTextAttr($value, $data)
    {
        $tags = $value ?: ($data['tags'] ?? '');
        return ConfigValueModel::getTagsText($tags);
    }


    public function getImagesAttr($value, $data)
    {
        $value = $value ?: ($data['images'] ?? []);
        $images = $value ? json_decode($value, true) : [];
        foreach ($images as $k =>$v) {
            $images[$k] = cdnurl($v);
        }
        return $images;
    }

    public function getHouseTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['house_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_HOUSE);
        return $list[$value] ?? '';
    }

    public function getPaymentTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['payment_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_PAYMENT);
        return $list[$value] ?? '';
    }

    public function getRentTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['rent_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_RENT);
        return $list[$value] ?? '';
    }



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['create_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
